# coding:utf-8
# __author:Xu.rt
# date:2017/9/14
import pickle, os
usr = {}
def input_file(dict_usr):
    if not os.path.exists('./usr.txt'):
        with open('usr.txt', 'wb') as usr_file:
            pickle.dump(dict_usr, usr_file)
    else:
        with open('usr.txt', 'rb') as usr_file:
            usr_dict = pickle.load(usr_file)
            usr_dict.update(dict_usr)
            print(usr_dict.items())
        with open('usr.txt', 'wb') as input_file:
            pickle.dump(usr_dict, input_file)

def login_user():
    usr_name = input('请输入用户名：')
    usr_passwd = input('请输入密码：')
    usr[usr_name] = usr_passwd

def chat_user():
    i = 0
    if not os.path.exists('./usr.txt'):
        print('请先注册用户！')
    else:
        with open('usr.txt', 'rb') as chat_file:
            user_dict = pickle.load(chat_file)
        while True:
            if i == 3:
                print('已经错误三次，请一秒钟后重新登陆')
                break
            usr_name = input('请输入用户名：')
            usr_passwd = input('请输入密码：')
            if (usr_name in user_dict.keys()) and (user_dict[usr_name] == usr_passwd):
                print('欢迎回来！！正在加载…………')
                break
            else:
                i += 1
                print('账号或密码错误，请重新登陆,已经输错', i, '次')


gui = '''
1、注册账号
2、用户登陆
3、退出
'''
while True:
    print(gui)
    usr_seclt = input('请输入你需要的功能（1-3）： ')
    if usr_seclt == '1':
        login_user()
        input_file(usr)
    elif usr_seclt == '2':
        chat_user()
    else:
        print('后会有期，你是最棒的！！！')
        break





